@extends('Admin.base')
@section('css')

@endsection
@section('style')

@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>部门管理</legend>
            </fieldset>
        </div>
        <div class="layui-card-body">
            <table id="LAY-table" lay-filter="LAY-table"></table>

            <script type="text/html" id="tableToolbar">
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
                    删除
                </a>
            </script>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <a class="layui-btn" href="{{ route('staff.add') }}">添 加</a>
                </div>
            </script>
        </div>
    </div>
@endsection

@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['form', 'table', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table

            table.render({
                elem: '#LAY-table'
                , toolbar: '#toolbarDemo'
                , url: "{{route('staff.department')}}"
                , method: "post"
                , page: true //开启分页
                , limit: 10
                , cols: [[
                    {field: 'id', title: 'ID',}
                    , {field: 'department_name', title: '部门名称',}
                    , {field: 'number', title: '部门编号',}
                    , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#tableToolbar'}
                ]]

            });


            //监听工具条
            table.on('tool(LAY-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('确认删除行吗?', function (index) {
                        $.ajax({
                            url: '/admin/adminer/' + data.id
                            , type: 'POST'
                            , dataType: 'json'
                            , data: {'_method': "DELETE"}
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message, {icon: 1, time: 2000});
                                    obj.del();
                                    layer.close(index);
                                    return false;
                                }
                                layer.msg(ret.message, {icon: 2});
                            }
                        });
                    });
                }
            });

            //监听搜索
            form.on('submit(LAY-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload('LAY-table', {
                    where: field, page: {curr: 1}
                });
            });
        });
    </script>

@endsection